package smartclass.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.FinalValues;
import smartclass.entity.DocumentInfo;
import smartclass.present.LogInActivity;

public class DocumentDataDao {

	static DocumentDataDao documentDao;
	ManagerDataBaseHelper db;
	String tableName;

	public DocumentDataDao() {
		// TODO Auto-generated constructor stub
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.DOCUMENT_TABLE;
	}

	public static DocumentDataDao getInstance() {
		if (documentDao == null) {
			documentDao = new DocumentDataDao();
		}
		return documentDao;
	}

	// Add Item
	public boolean addDocument(DocumentInfo doc) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		if (String.valueOf(doc.getIdTopic()).isEmpty()) {
			return false;
		}
		ContentValues value = new ContentValues();
		value.put(FinalValues.NAME, doc.getName());
		value.put(FinalValues.DATE_CREAT, doc.getDateCreate());
		value.put(FinalValues.ID_TOPIC, doc.getIdTopic());
		value.put(FinalValues.PATH, doc.getPath());
		value.put(FinalValues.DOCUMENT_INFO, doc.getDocumentInfo());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	// Get Item
	public DocumentInfo getDocument(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		DocumentInfo docInfo = new DocumentInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ FinalValues.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			docInfo.setId(Integer.parseInt(c.getString(0)));
			docInfo.setName(c.getString(1));
			docInfo.setDateCreate(c.getString(2));
			docInfo.setIdTopic(c.getInt(3));
			docInfo.setPath(c.getString(4));
			docInfo.setDocumentInfo(c.getString(5));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return docInfo;
	}

	// Get All Item
	public ArrayList<DocumentInfo> getAllDocument() {
		ArrayList<DocumentInfo> allDocument = new ArrayList<DocumentInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = data.rawQuery(sql, null);
		DocumentInfo docInfo = null;
		if (c.moveToFirst()) {
			do {
				docInfo = new DocumentInfo();
				docInfo.setId(c.getInt(0));
				docInfo.setName(c.getString(1));
				docInfo.setDateCreate(c.getString(2));
				docInfo.setIdTopic(c.getInt(3));
				docInfo.setPath(c.getString(4));
				docInfo.setDocumentInfo(c.getString(5));
				allDocument.add(docInfo);
			} while (c.moveToNext());
		}
		return allDocument;
	}

	// Edit Item

	public boolean updateDocument(DocumentInfo doc) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues values = new ContentValues();
		values.put(FinalValues.NAME, doc.getName());
		values.put(FinalValues.DATE_CREAT, doc.getDateCreate());
		values.put(FinalValues.ID_TOPIC, doc.getIdTopic());
		values.put(FinalValues.PATH, doc.getPath());
		values.put(FinalValues.DOCUMENT_INFO, doc.getDocumentInfo());

		data.update(tableName, values, FinalValues.ID + "=?",
				new String[] { String.valueOf(doc.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + FinalValues.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}
}
